import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:series_play/comm/base/base_state.dart';
import 'package:series_play/widget/app_color.dart';
import 'package:series_play/widget/app_image.dart';
import 'package:series_play/widget/app_refresh_view.dart';
import 'package:series_play/widget/app_text.dart';

import 'controller/home_latest_controller.dart';

/// 首页最新列表
class HomeLatestPage extends StatefulWidget {
  const HomeLatestPage({super.key});

  @override
  State<HomeLatestPage> createState() => _HomeLatestPageState();
}

class _HomeLatestPageState
    extends BaseControllerState<HomeLatestPage, HomeLatestController> {
  @override
  void initState() {
    super.initState();
    mController.onRefresh();
  }

  @override
  Widget buildPage(BuildContext context) {
    final itemWidth = (MediaQuery.of(context).size.width - 18.w * 2 - 20.w) / 3;
    final imgHeight = itemWidth * 5 / 4;
    return AppRefreshGridView(
      padding: const EdgeInsets.symmetric(horizontal: 18).w,
      listController: mController,
      crossAxisCount: 3,
      crossAxisSpacing: 10.w,
      mainAxisSpacing: 10.w,
      itemBuilder: (context, index) {
        final item = mController.dataList[index];
        return Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            ClipRRect(
              borderRadius: BorderRadius.circular(8).r,
              child: AppImage(
                item.cover ?? '',
                width: itemWidth,
                height: imgHeight,
                boxFit: BoxFit.cover,
              ),
            ),
            8.verticalSpace,
            AppText(
              item.name ?? '',
              color: AppColor.white,
              maxLines: 2,
              overflow: TextOverflow.ellipsis,
            ),
          ],
        );
      },
      onRefresh: mController.onRefreshing,
      onLoad: mController.onLoadingMore,
    );
  }

  @override
  HomeLatestController createController() {
    return HomeLatestController();
  }

  @override
  bool hideAppbar() {
    return true;
  }

  @override
  bool get wantKeepAlive => true;

  @override
  Color getBackgroundColor() {
    return Colors.transparent;
  }
}
